<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="校区" prop="campus" label-width="30">
        <el-select v-model="queryParams.campus" style="width: 120px" placeholder="请选择校区" clearable>
          <el-option
            v-for="dict in dict.type.sys_campus_choose"
            :key="dict.value"
            :label="dict.label"
            :value="dict.label"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="教师名字" prop="teacherName">
        <el-input
          style="width: 150px"
          v-model="queryParams.teacherName"
          placeholder="请输入教师名字"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="学科" prop="subject">
        <el-select v-model="queryParams.subject" style="width: 150px" placeholder="请选择学科" clearable>
          <el-option
            v-for="dict in dict.type.sys_teach_subject"
            :key="dict.value"
            :label="dict.label"
            :value="dict.label"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="老师类型" prop="type">
        <el-select v-model="queryParams.type" style="width: 150px" placeholder="请选择老师类型" clearable>
          <el-option
            v-for="dict in dict.type.sys_teacher_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.label"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="日报时间" prop="paperTime">
        <el-date-picker clearable
          style="width: 180px"
          v-model="queryParams.paperTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择日报时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-bell"
          size="mini"
          @click="handleRemind"
        >日报一键提醒</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['teacher:newspaper:add']"
        >新增日报</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['teacher:newspaper:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['teacher:newspaper:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['teacher:newspaper:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="newspaperList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="20" align="center" />
      <el-table-column label="教师名字" align="center"  width="150" prop="teacherName" />
      <el-table-column label="日报时间" align="center" width="150" prop="paperTime" />
      <el-table-column label="创建时间" align="center" width="200" prop="createTime" />
      <el-table-column label="备注" align="left" width="700" prop="intro">
        <template slot-scope="scope">
        <el-tag type="success">{{scope.row.intro}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['teacher:newspaper:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['teacher:newspaper:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!--查看老师日报-->
    <el-dialog title="日报查看" :visible.sync="newsPaperformOpen" width="500px" append-to-body>
      <el-form ref="form" :model="newsPaperform" label-width="80px">
        <el-form-item label="日报时间" prop="paperTime">
          {{newsPaperform.paperTime}}
        </el-form-item>
        <el-form-item label="日报老师" prop="note">
          {{newsPaperform.teacherName}}
        </el-form-item>
        <el-form-item label="日报内容" prop="state">
          <el-input
            type="textarea"
            :rows="2"
            v-model="newsPaperform.intro">
          </el-input>
        </el-form-item>
      </el-form>
    </el-dialog>

    <!-- 添加或修改老师日报对话框 -->
    <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" width="1100px" append-to-body>
      <el-alert
        title="特别注意"
        type="error"
        show-icon
        description="1.必须要写学生教学跟踪情况  2.差生要加入重点关注   3.日报内容复制一份到教学跟踪表excel表格以及老师沟通群"
        :closable="false" />
      <br/>
      <el-form :model="form" ref="form" :rules="rules" label-width="80px" >
        <el-form-item label="日报时间" prop="logTime">
          <el-date-picker clearable
                          v-model="form.paperTime"
                          type="date"
                          style="width: 200px !important;"
                          value-format="yyyy-MM-dd"
                          :picker-options="pickSelectStart"
                          placeholder="请选择登记时间">
          </el-date-picker>
        </el-form-item>

        <el-form-item v-for="eduadmin in eduadminForm" label="教学追踪">
          <el-select v-model="eduadmin.className" placeholder="请选择班级"
                     @change="classChange" >
            <el-option
              v-for="(item,index) in classList"
              :key="index"
              :label="item.name"
              :value="item.name"
              >
            </el-option>
          </el-select>
          -
          <el-select filterable
            v-model="eduadmin.studentName" placeholder="请选择学生">
            <el-option
              v-for="(item,index) in studentNameList"
              :key="index"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
          -
          <el-select v-model="eduadmin.problemType" placeholder="问题类型" style="width: 150px">
            <el-option
              v-for="dict in dict.type.sys_problem_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
          -
          <el-date-picker clearable style="width: 170px"
            v-model="eduadmin.happenTime"
            type="datetime"
            placeholder="选择日期时间"
            default-time="12:00:00">
          </el-date-picker>
          -
          <el-input v-model="eduadmin.deductMarks" placeholder="扣分" style="width: 60px !important;"></el-input>
          <div style="height: 32px;line-height: 32px;">
            <el-input v-model="eduadmin.remark" style="top: 4px;width: 725px;height: 32px;"
                      type="textarea"
                      :rows="1"
                      placeholder="情况描述" />
            &nbsp;<el-button  type="success" icon="el-icon-plus" style="height: 33px;margin-top: 4px"
                              @click="addEduadmin(eduadmin)">加一行</el-button>
            &nbsp;<el-button  type="danger" icon="el-icon-plus" style="height: 33px;margin-top: 4px"
                              @click="removeEduadmin(eduadmin)">减一行</el-button>
          </div>
          </el-form-item>
      <el-divider></el-divider>

        <el-form-item v-for="employment in employmentForm" label="就业追踪">
          <el-select v-model="employment.className" @change="classChange" placeholder="请选择班级">
            <el-option
              v-for="(item,index) in classList"
              :key="index"
              :label="item.name"
              :value="item.name"
            >
            </el-option>
          </el-select>
          -
          <el-select multiple filterable v-model="employment.studentNames"
                     placeholder="请选择学生">
              <el-option
                v-for="(item,index) in studentNameList"
                :key="index"
                :label="item.name"
                :value="item.id">
              </el-option>
          </el-select>
          -
          <el-select v-model="employment.traceGoal" placeholder="请选择跟踪目的" style="width: 150px">
            <el-option
              v-for="dict in dict.type.sys_trace_goal"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
          -
          <el-date-picker clearable style="width: 170px"
            v-model="employment.traceTime"
            type="datetime"
            placeholder="选择日期时间"
            default-time="12:00:00">
          </el-date-picker>
          -
          <el-select v-model="employment.studentState" placeholder="请选择工作状态" style="width: 150px">
            <el-option
              v-for="dict in dict.type.sys_employment_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
          <div style="height: 32px;line-height: 32px;">
            <el-input style="top: 4px;width: 725px;height: 32px;"
                      type="textarea"
                      :rows="1"
                      v-model="employment.note"
                      placeholder="情况描述" />
            &nbsp;<el-button  type="success" icon="el-icon-plus" style="height: 33px;margin-top: 4px"
                              @click="addEmployment(employment)">加一行</el-button>
            &nbsp;<el-button  type="danger" icon="el-icon-plus" style="height: 33px;margin-top: 4px"
                              @click="removeEmployment(employment)">减一行</el-button>
          </div>
        </el-form-item>
        <el-divider></el-divider>
        <el-form-item label="日报内容" prop="remark">
          <el-input v-model="form.intro"
                    :rows="3"
                    type="textarea"
                    placeholder="对今日工作的总结和明日工作的安排" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="success" @click="submitForm">提交日报</el-button>
        <el-button @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listNewspaper, getNewspaper, delNewspaper, addNewspaper, dailyReminder } from "@/api/teacher/newspaper";
import {getStudentMessage, listClassName, getStudentByClassId, listStudentMessage} from "@/api/student/studentMessage";

export default {
  dicts: ['sys_problem_type','sys_trace_goal','sys_employment_status','sys_teach_subject','sys_campus_choose','sys_teacher_type'],
  name: "Newspaper",
  data() {
    return {
      value3: '',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 老师日报表格数据
      newspaperList: [],
      eduadminClasss: [],
      attentionList: [],
      studentNameList: [],
      test: [
        {class: "java", studentNames: ['张三', '李四']},
        {class: "ui", studentNames: ['王五', 'zs']}
      ],
      classList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      newsPaperformOpen: false,
      newsPaperform: {},
      pickSelectStart: {
        disabledDate: (time) => {
          //return time.getTime() < Date.now(); //可选未来天
          //return time.getTime() > Date.now();  // 可选历史天、可选当前天、不可选未来天
          if (time.getTime() > Date.now()) return true;
          // return time.getTime() > Date.now() - 8.64e7;  // 可选历史天、不可选当前天、不可选未来天
          return time.getTime() < Date.now() - 2 * 8.64e7;  // 不可选历史天、可选当前天、可选未来天
          // return time.getTime() < Date.now(); // 不可选历史天、不可选当前天、可选未来天
        }
      },

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        teacherName: null,
        paperTime: null,
        intro: null,
      },
      form: {},
      // 表单参数
      eduadminForm: [{}],
      employmentForm: [{}],
      // 表单校验
      rules: {
      },

    };
  },
  created() {
    this.getList();
    this.getClassList();
    // this.getListAttention();
  },
  methods: {
    /** 查询老师日报列表 */
    getList() {
      this.loading = true;
      listNewspaper(this.queryParams).then(response => {
        this.newspaperList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getClassList() {
      listClassName().then(response => {
        this.classList = response.rows;
        console.log(this.classList)
      });
    },
    // classChange(value) {
    //   getStudentByClassId().then(response => {
    //     this.test = response;
    //     for (let i in this.test) {
    //         if (this.test[i].name == value) {
    //           this.studentNameList = this.test[i].names;
    //         }
    //       }
    //     console.log(this.studentNameList);
    //   });
    // },

    /** 查询学生信息列表 */
    getListStudentMessage() {
      listStudentMessage().then(response => {
        this.studentNameList = response.rows;
      });
    },

      // getStudentMessage(id).then(response => {
      //   if (response.data != null) {
      //     this.studentNameList[0] = [],
      //     this.studentNameList[0] = response.data
      //   } else {
      //     this.studentNameList = [];
      //   }
      //   console.log(this.studentNameList);
      // });

    // getListAttention() {
    //   listAttention().then(response => {
    //     this.attentionList = response.rows;
    //     console.log(this.attentionList);
    //   });
    // },


    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.getListStudentMessage();
      this.open = true;
      this.title = "添加老师日报";
      this.form.paperTime = new Date();
    },
    handleView(row) {
      this.newsPaperform = row;
      this.newsPaperformOpen = true;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getNewspaper(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改老师日报";
      });
    },
    /** 提交按钮 */
    submitForm() {
        this.form.eduadminForm = this.eduadminForm;
        this.form.employmentForm = this.employmentForm;
        addNewspaper(this.form).then(response => {
          let {code, msg} = response;
          if (code === -1) {
            this.$modal.msgError(msg);
          } else {
            this.$modal.msgSuccess(msg);
          }
          this.getList();
        });
      this.open = false;
      this.form = {};
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除老师日报编号为"' + ids + '"的数据项？').then(function() {
        return delNewspaper(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('teacher/newspaper/export', {
        ...this.queryParams
      }, `newspaper_${new Date().getTime()}.xlsx`)
    },

    addEmployment(employment) {
      employment = {};
      this.employmentForm.push(employment);
    },

    removeEmployment(employment){
      for(let i = 0; i < this.employmentForm.length ; i++){
        if(this.employmentForm[i].index == employment.index){
          this.employmentForm.splice(i,1);
          break;
        }
      }
    },
    addEduadmin(eduadmin) {
      eduadmin = {};
      this.eduadminForm.push(eduadmin);
    },
    removeEduadmin(eduadmin){
      for(let i = 0; i < this.employmentForm.length ; i++){
        if(this.eduadminForm[i].index == eduadmin.index){
          this.eduadminForm.splice(i,1);
          break;
        }
      }
    },
    handleRemind() {
      dailyReminder().then(response => {
          this.$modal.msgSuccess("提醒成功！");
      });
    }
  }
};
</script>
